﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class master : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        NVR.BLL.UserInfo userInfo = new NVR.BLL.UserInfo();
        if (!IsPostBack)
        {
            if (Session["username"]!=null)
            {
                user.Text = Session["username"].ToString();
                userkind.Text = userInfo.GetUserType(Session["username"].ToString());
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string searchType = this.lblsb.Text;//得到搜索类型
        string txt = this.txtIput.Text;
        if (txt != "")
        {
            if (searchType == "用户搜索")
            {
                NVR.Entity.userInfo user = new NVR.Entity.userInfo();
                NVR.BLL.UserInfo userInfo = new NVR.BLL.UserInfo();
                NVR.BLL.NVRDevInfo nvr = new NVR.BLL.NVRDevInfo();
                user = userInfo.GetUserInfo(txt);
                if (user == null)   //搜索的用户不存在
                {
                    ((LinkButton)this.placeRight.FindControl("linkbtnUser")).Visible = false;
                    ((Label)this.placeRight.FindControl("lblUser")).Visible = false;
                    ((Label)this.placeRight.FindControl("lblRegDate")).Visible = false;
                    ((Label)this.placeRight.FindControl("lblSbNum")).Visible = false;
                    ((Label)this.placeRight.FindControl("lblTel")).Visible = false;
                    ((Label)this.placeRight.FindControl("lblQQ")).Visible = false;
                    ((Label)this.placeRight.FindControl("lblEmail")).Visible = false;
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "nouser", "<script type='text/javascript'>alert('您查找的用户不存在')</script>");
                }
                else
                {
                    if (this.userkind.Text == "超级管理员")
                    {
                        ((LinkButton)this.placeRight.FindControl("linkbtnUser")).Visible = true;
                        ((Label)this.placeRight.FindControl("lblUser")).Visible = true;
                        ((Label)this.placeRight.FindControl("lblRegDate")).Visible = true;
                        ((Label)this.placeRight.FindControl("lblSbNum")).Visible = true;
                        ((Label)this.placeRight.FindControl("lblTel")).Visible = true;
                        ((Label)this.placeRight.FindControl("lblQQ")).Visible = true;
                        ((Label)this.placeRight.FindControl("lblEmail")).Visible = true;

                        ((LinkButton)this.placeRight.FindControl("linkbtnUser")).Text = txt;
                        ((Label)this.placeRight.FindControl("lblUser")).Text = txt;
                        ((Label)this.placeRight.FindControl("lblRegDate")).Text = user.regDate.ToString();
                        ((Label)this.placeRight.FindControl("lblSbNum")).Text = nvr.GetNvrNums(txt).ToString();
                        ((Label)this.placeRight.FindControl("lblTel")).Text = user.tel;
                        ((Label)this.placeRight.FindControl("lblQQ")).Text = user.qq;
                        ((Label)this.placeRight.FindControl("lblEmail")).Text = user.email;
                    }
                    else
                    {
                        //没有权限查看
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "userqx", "<script type='text/javascript'>alert('您没有权限查看其他用户信息')</script>");
                    }
                }
            }
            if (searchType == "设备搜索")
            {
                NVR.BLL.DeviceInfo dev = new NVR.BLL.DeviceInfo();
                NVR.Entity.DeviceInfo devInfo = new NVR.Entity.DeviceInfo();
                devInfo = dev.GetDeviceInfo(txt);
                if (devInfo == null)  //设备不存在
                {
                    //((GridView)this.placeRight.FindControl("grvTable")).Visible = false;
                    //this.txtIput.Text = "";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "nosn", "<script type='text/javascript'>alert('您查找的设备不存在')</script>");
                }
                else
                {
                    NVR.BLL.DeviceTable dt = new NVR.BLL.DeviceTable();
                    List<NVR.Entity.DeviceTable> list = dt.GetDeviceTable(txt);
                    if (list.Count > 0)
                    {
                        if (((GridView)this.placeRight.FindControl("grvTable"))==null)   //是不是在main.aspx页面查找
                        {
                            Response.Redirect("main.aspx?sn="+txt);
                        } 
                        else
                        {
                            ((GridView)this.placeRight.FindControl("grvTable")).DataSource = list;
                            ((GridView)this.placeRight.FindControl("grvTable")).DataBind();  
                        } 
                    }
                }
            }
        }
    }
    protected void imgQuit_Click(object sender, ImageClickEventArgs e)
    {
        Session["username"] = null;
        Response.Redirect("default.aspx");
    }
}
